//
//  UseRulesVC.m
//  ShareElectricity
//
//  Created by sunsoft on 2017/6/6.
//  Copyright © 2017年 sunnsoft.HH. All rights reserved.
//

#import "UseRulesVC.h"
#import "UseRulesCell.h"

@interface UseRulesVC ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSInteger indexCell;
    NSInteger sectionCell;
    BOOL isSelected;
}
@property (nonatomic,strong)NSArray * cellTitles;
@property (nonatomic,strong)NSBundle *bundle;
@property(nonatomic,strong)UITableView * tableView;
@end

@implementation UseRulesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.cellTitles = @[@"借出的移動電源遺失怎麽辦", @"借出的移動電源是壞的怎麽辦", @"退款規則"];
    [SEChangeLanguage initUserLanguage];
    _bundle = [SEChangeLanguage bundle];
    __weak typeof(self) weakSelf = self;
    [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"使用規則" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } aimView:self.view];
     [self initTableview];
    
}
-(void)initTableview{
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = GrayColor;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 40;
    [self.tableView registerClass:[UseRulesCell class] forCellReuseIdentifier:@"useRulesCell"];
    [self.view addSubview:self.tableView];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UseRulesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == sectionCell) {
        
        if (indexPath.row == indexCell) {
            if (cell.isSelect == YES) {
                cell.arrowView.image = [UIImage imageNamed:@"Rules_icon"];
                cell.texLabel.numberOfLines = 1;
                cell.isSelect = NO;
                isSelected = NO;
                [self.tableView reloadData];
                return;
            }
        }
    }
    cell.arrowView.image = [UIImage imageNamed:@"Rules_icon_down"];
    isSelected = YES;
    cell.isSelect =YES;
    indexCell = indexPath.row;
    sectionCell = indexPath.section;
    [self.tableView reloadData];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return 1;
            break;
            
        default:
             return 3;
            break;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UseRulesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"useRulesCell"];
    
    if (indexPath.section == 0) {
        
        cell.titleLabel.text = [_bundle localizedStringForKey:@"按金規則" value:nil table:@"Localization"];
        cell.lineView.hidden = YES;
    }else{
        
        NSString *title = self.cellTitles[indexPath.row];
        cell.titleLabel.text = [_bundle localizedStringForKey:title value:nil table:@"Localization"];
        
    }
    if (isSelected == YES) {
        
        if (indexPath.section == sectionCell) {
            if (indexPath.row == indexCell) {
                
                cell.texLabel.numberOfLines = 0;
            }else{
                
                cell.texLabel.numberOfLines = 1;
                cell.arrowView.image = [UIImage imageNamed:@"Rules_icon"];
            }
        }else{
            
            cell.texLabel.numberOfLines = 1;
            cell.arrowView.image = [UIImage imageNamed:@"Rules_icon"];
        }
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
     if (isSelected == YES) {
         if (indexPath.section == sectionCell) {
             if (indexPath.row == indexCell) {
                 
                 return UITableViewAutomaticDimension;
                 
             }else{
                 
                 return 40;
                 
                 
             }
         }else{
             
             return 40;
             
             
         }

     }else return 40;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * bgiew = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HHScreenWidth, 10)];
    bgiew.backgroundColor = GrayColor;
    return bgiew;
}
-(UITableView *)tableView{
    
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, HHScreenWidth, HHScreenHeight-64) style:UITableViewStylePlain];
    }
    
    return _tableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
